<?php
class settingController extends Web_Controller_Base
{
    public function init()
    {
        if(!Web_Model_Auth::hasRightSetting()){
            $this->forward('http://' . HOST);
        }
    }
    
    public function indexAction()
    {
        $this->memberAction();
    }
    
    public function memberAction()
    {
        $pageSize = Web_Config_Page::settingMemberPageSize();
        $navSize  = Web_Config_Page::settingMemberNavSize();
        $page = $this->getParam('page', 1);
        try {
            $limit = Web_Model_Navigate::getLimit($page, $pageSize);
            $members = Web_Model_Member::getAll($limit['start'], $limit['limit']);
            $pageNav = Web_Model_Navigate::getPageListBar($members['total'], $page, $pageSize, $navSize);
        }catch (Exception $e){
            exit('系统异常,稍后重试');
        }
        $this->setData('title', '会员设置');
        $this->setData('members', $members['data']);
        $this->setData('pageNav', $pageNav);
        $this->setView('Setting/member.tpl');
    }
    
    public function classAction()
    {
        try {
            $class = Web_Model_Class::getAllClass();
        }catch (Exception $e){
            exit('系统异常,稍后重试');
        }
        $this->setData('title', '分类设置');
        $this->setData('class', $class);
        $this->setView('Setting/class.tpl');
    }
    
    public function commentAction()
    {
        $pageSize = Web_Config_Page::settingCommentPageSize();
        $navSize  = Web_Config_Page::settingCommentNavSize();
        $page = $this->getParam('page', 1);
        try {
            $limit = Web_Model_Navigate::getLimit($page, $pageSize);
            $comments = Web_Model_Comment::getAll($limit['start'], $limit['limit']);
            $pageNav = Web_Model_Navigate::getPageListBar($comments['total'], $page, $pageSize, $navSize);
        }catch (Exception $e){
            exit('系统异常,稍后重试');
        }
        $this->setData('title', '评论设置');
        $this->setData('comments', $comments['data']);
        $this->setData('pageNav', $pageNav);
        $this->setView('Setting/comment.tpl');
    }
    
    public function feedbackAction()
    {
        $pageSize = Web_Config_Page::settingFeedbackPageSize();
        $navSize  = Web_Config_Page::settingFeedbackNavSize();
        $page = $this->getParam('page', 1);
        try {
            $limit = Web_Model_Navigate::getLimit($page, $pageSize);
            $feedbacks = Web_Model_Feedback::getAll($limit['start'], $limit['limit']);
            $pageNav = Web_Model_Navigate::getPageListBar($feedbacks['total'], $page, $pageSize, $navSize);
        }catch (Exception $e){
            exit('系统异常,稍后重试');
        }
        $this->setData('title', '意见反馈');
        $this->setData('feedbacks', $feedbacks['data']);
        $this->setData('pageNav', $pageNav);
        $this->setView('Setting/feedback.tpl');
    }
    
    public function updateClassAjaxAction()
    {
        $classID   = $this->getPost('class_id', '');
        $classSort = $this->getPost('class_sort', '');
        $className = $this->getPost('class_name', '');
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            Web_Model_Class::updateClass($classID, $classSort, $className);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常，稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
    
    public function addMainClassAjaxAction()
    {
        $classSort = $this->getPost('class_sort', '');
        $className = $this->getPost('class_name', '');
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            Web_Model_Class::addMainClass($classSort, $className);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常，稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
    
    public function addSubClassAjaxAction()
    {
        $classPID   = $this->getPost('class_pid', '');
        $classSort = $this->getPost('class_sort', '');
        $className = $this->getPost('class_name', '');
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            Web_Model_Class::addSubClass($classPID, $classSort, $className);
        }catch (Eren_DB_Exception $e){
            echo $e->getMessage();
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常，稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
    
    public function delClassAjaxAction()
    {
        $classID   = $this->getPost('class_id', '');
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            Web_Model_Class::delClass($classID);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常，稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
    
    public function setMemberTypeAjaxAction()
    {
        $memberID = $this->getPost('member_id', '');
        $memberType = $this->getPost('member_type', 0);
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            Web_Model_Member::setType($memberID, $memberType);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常，稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
    
    public function setCommentTypeAjaxAction()
    {
        $commentID = $this->getPost('comment_id', '');
        $commentType = $this->getPost('comment_type', 0);
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            Web_Model_Comment::setType($commentID, $commentType);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常，稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
    
    public function delFeedbackAjaxAction()
    {
        $feedbackID   = $this->getPost('feedback_id', '');
        $ret['errCode']=0;
        $ret['errMsg'] ='success';
        try {
            Web_Model_Feedback::delete($feedbackID);
        }catch (Eren_DB_Exception $e){
            $ret['errCode'] = -10;
            $ret['errMsg']  = '系统异常，稍后重试';
        }catch (Exception $e){
            $ret['errCode'] = $e->getCode();
            $ret['errMsg']  = $e->getMessage();
        }
        echo json_encode($ret);
    }
}